//
//  KLZheKouDetailViewController.m
//  KunLun
//
//  Created by Mac on 2018/1/16.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "KLZheKouDetailViewController.h"
#import "KLZhekouFooterView.h"
#import "KLOrderTableViewCell.h"
#import "STTextView.h"
#import "STPhotoUrlImageBrowerController.h"
#import "STPickerViewController.h"
#import "STActionSheet.h"
#import "PHPhotoLibrary+STTools.h"
@interface KLZheKouDetailViewController ()<STPhotoUrlImageBrowerControllerDelegate,STActionSheetDelegate>
@property(nonatomic, strong) STTextView                     *textView;
@property(nonatomic, strong) NSString                     *imageUrl;
@property(nonatomic, strong) STButton                     *imageButton;
@property(nonatomic, strong) STPhotoUrlImageBrowerController                     *pickerVC;
@property(nonatomic, strong) STUrlPhotoModel                     *imageModel;
@end

@implementation KLZheKouDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"匀加速审核";
    [self.tableView st_autoAdjustAllResponder];
    [self.tableView registerClass:[KLZhekouFooterView class] forHeaderFooterViewReuseIdentifier:@"footer"];
    [self.tableView registerClass:[KLOrderTableViewCell class] forCellReuseIdentifier:@"cell"];
    // Do any additional setup after loading the view.
    [self sendOrderDetailRequets];
    

}
#pragma mark --subView
- (void)configSubView{
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 220)];
    footer.backgroundColor = [UIColor whiteColor];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 10, UIScreenWidth - 20, 40)
                                                     text:@"匀加速截图"
                                                textColor:KL_firstTextColor
                                                     font:16
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [footer addSubview:titleLable];
    
    __weak typeof(self) weakSelf =  self;
    self.imageButton = [[STButton alloc] initWithFrame:CGRectMake(10, titleLable.bottom, 80, 80)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    [self.imageButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedIconButton];
    }];
    [self.imageButton sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] forState:UIControlStateNormal];
    [footer addSubview:self.imageButton];
    
    self.textView = [[STTextView alloc] initWithFrame:CGRectMake(10,self.imageButton.bottom + 10, UIScreenWidth - 20, 120)];
    self.textView.placeholder = @"如果您要拒绝，请输入拒绝理由";
    self.textView.backgroundColor = KL_backgroundColor;
    [footer addSubview:self.textView];
    footer.height = self.textView.bottom + 10;
    self.tableView.tableFooterView = footer;
    
    CGFloat top = [KLUtils tabBarTop];

    STButton * sendButton = [[STButton alloc] initWithFrame:CGRectMake(0, top, UIScreenWidth / 2, 49)
                                                     title:@"通过"
                                                titleColor:FlatWhite
                                                 titleFont:18
                                              cornerRadius:0
                                           backgroundColor:KL_BlueBackGroundColor
                                           backgroundImage:nil
                                                     image:nil];
    [sendButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedSendButton];
    }];
    sendButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.view addSubview:sendButton];
    
    STButton * refuseButton = [[STButton alloc] initWithFrame:CGRectMake(UIScreenWidth / 2, top, UIScreenWidth / 2, 49)
                                                      title:@"拒绝"
                                                 titleColor:FlatWhite
                                                  titleFont:18
                                               cornerRadius:0
                                            backgroundColor:KL_redColor
                                            backgroundImage:nil
                                                      image:nil];
    [refuseButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedRefuseButton];
    }];
    refuseButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.view addSubview:refuseButton];
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [KLOrderTableViewCell CellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return [KLZhekouFooterView footerHeight] - 80;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    KLZhekouFooterView * footer = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footer"];
    footer.shopModel = self.shopModel;
    footer.menuButton.hidden = YES;
    footer.clipsToBounds = YES;
    return footer;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KLOrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    KLOrderGoodModel *  model = self.shopModel.goodsorder.firstObject;
    model.goods_num = @(1);
    cell.shopModel = model;
    cell.moneyLable.text = @"折扣券";
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    KLOrderGoodModel *  model = self.shopModel.goodsorder.firstObject;
//    KLGoodDetailViewController * vc = [KLGoodDetailViewController new];
//    vc.goodID = model.goods_id.description;
//    [self.navigationController pushViewController:vc animated:YES];
//    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedSendButton{
    [self showAlertCancelAndConfirm:@"您确定要审核通过吗?" andWithBlock:^(NSString *name) {
        if ([name isEqualToString:@"确认"]) {
             [self sendRefusuRequestWithType:@"2"];
        }
    }];
}
- (void)onSelctedRefuseButton{
    if (!self.textView.text.length) {
        [SVProgressHUD showErrorWithStatus:@"您还未输入拒绝理由"];
        return;
    }
    [self sendRefusuRequestWithType:@"3"];
}
- (void)onSelctedIconButton{
    STUrlPhotoModel * model = [STUrlPhotoModel new];
    model.thumbImage = self.imageButton.currentImage;
    model.thumbImageUrl = self.imageUrl;
    self.imageModel = model;
    self.pickerVC = [[STPhotoUrlImageBrowerController alloc] initWithArray:@[model] curentIndex:0];
    self.pickerVC.STPhotoUrlImageBrowerControllerdelegate = self;
    self.pickerVC.themeColor = KL_BlueBackGroundColor;
    self.pickerVC.shouldHideBottomView = YES;
    [self presentViewController:self.pickerVC animated:NO completion:nil];
}
#pragma mark --STPhotoUrlImageBrowerControllerDelegate
- (UIView*)STPhotoSystemBrowserControllerDidScrollToIndexpath:(NSIndexPath*)indexPath model:(STUrlPhotoModel*)model{
    return self.imageButton;
}
- (void)rightBarActionFromController:(STPhotoUrlImageBrowerController*)controller currentIndexPath:(NSIndexPath *)curentIndexpath{
    
    STActionSheet * actionSheet = [[STActionSheet alloc] initWithTitle:nil
                                                          buttonTitles:@[@"保存图片"] cancelTitle:@"取消"
                                                              delegate:self
                                   ];
    [actionSheet show];
}
#pragma mark --STActionSheetDelegate
- (void)actionSheet:(STActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [PHPhotoLibrary saveImageToAssetsLibrary:self.imageButton.currentImage
                                     libraryName:@"昆仑科技" successHandle:^{
                                         [SVProgressHUD showSuccessWithStatus:@"保存到相册成功"];
                                     } errorHandle:^(STSaveImageError error) {
                                         
                                     }];
    }
}
#pragma mark --NetWork Method
- (void)sendOrderDetailRequets{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/shop/screenshotinfo",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    [dic setObject:self.shopModel.orderID forKey:@"screenshot_id"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 NSString * imageUrl = dataDic[@"screenshot"];
                                                 self.imageUrl = imageUrl;
                                                 [self configSubView];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 
                                                 
                                             }];
}
- (void)sendRefusuRequestWithType:(NSString*)type{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/shop/examinescreenshot",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    [dic setObject:self.shopModel.orderID forKey:@"screenshot_id"];
    if ([type isEqualToString:@"2"]) {
        
    }else{
        //拒绝
        if (self.textView.text) {
            [dic setObject:_textView.text forKey:@"reason"];
        }
    }
    [dic setObject:type forKey:@"type"];

    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 [SVProgressHUD showSuccessWithStatus:@"拒绝成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 
                                                 
                                             }];
}
@end
